package com.example.mqspring.rabiitmqbasicleverone.rabbitmqTopic;

import com.example.mqspring.utils.RabbitMqUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * topic模式和direct差不多,区别就是routkey
 * topic的routkey可以用模板匹配 * 匹配一个单词
 * #匹配0个或多个单词
 */
public class Consumer extends DefaultConsumer {

    static String myexchange = "MY_TOPIC_EXCHANGE";
    static private  String ROUTKEY = "*_EXCHANGE_KEY";
    static private  String queue = "TOPIC_QUEUE";
    /**
     * @param channel the channel to which this consumer is attached
     */
    public Consumer(Channel channel) {
        super(channel);
    }

    public Channel declareConsumer(String queue,Channel channel, String MyExchange, String routingkey) throws Exception {
        return RabbitMqUtil.declareConsumer(queue,"topic",channel,MyExchange,routingkey);
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String s = new String(body);
    }

    public static void main(String[] args) {

        Connection connection = RabbitMqUtil.createConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            Consumer consumer = new Consumer(channel);
            channel = consumer.declareConsumer(queue, channel, myexchange, ROUTKEY);
            String s = channel.basicConsume(queue, true, consumer);
            channel.basicCancel(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
